package com.etpjava.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.etpjava.dao.DepartmentDao;
import com.etpjava.model.Department;
import com.etpjava.model.Employee;
import com.etpjava.model.Manager;

public class DepartmentDaoImpl implements DepartmentDao {
	Configuration cfg=new Configuration().configure();
	SessionFactory sf=cfg.buildSessionFactory();
	Session session=null;
	
	@Override
	public boolean delete(Department dp) {
		try{
			session=sf.openSession();
			session.beginTransaction();
			session.delete(dp);
			session.getTransaction().commit();
			
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}finally{
			session.close();
		}
		return true;
	}

	@Override
	public boolean save(Department dp) {
		try{
			session=sf.openSession();
			session.beginTransaction();
			session.save(dp);
			session.getTransaction().commit();
			
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}finally{
			session.close();
		}
		return true;
	}

	@Override
	public boolean updata(Department dp) {
		try{
			session=sf.openSession();
			session.beginTransaction();
			session.update(dp);
			session.getTransaction().commit();
			
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
			return false;
		}finally{
			session.close();
		}
		return true;
	}
	
	@Override
	public List<Department> selectAll() {
		List<Department> contentLst = null;
		try{
			session=sf.openSession();
			session.beginTransaction();
			String sql="from Department ";
			Query query=session.createQuery(sql);
			contentLst=(List<Department>)query.list();
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		return contentLst;
	}

	@Override
	public List<Department> selectByCreateTime(Date time) {
		List<Department> contentLst = null;
		try{
			session=sf.openSession();
			session.beginTransaction();
			String sql="from Department d where d.buildTime = ?";
			Query query=session.createQuery(sql);
			query.setDate(0, time);
			contentLst=(List<Department>)query.list();
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		return contentLst;
	}

	@Override
	public Department selectById(int id) {
		Department dp = null;
		try{
			session=sf.openSession();
			session.beginTransaction();
			dp=(Department)session.get(Department.class, id);
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		return dp;
	}

	@Override
	public List<Department> selectByName(String name) {
		List<Department> contentLst = null;
		try{
			session=sf.openSession();
			session.beginTransaction();
			String sql="from Department d where d.name = ?";
			Query query=session.createQuery(sql);
			query.setString(0, name);
			contentLst=(List<Department>)query.list();
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		return contentLst;
	}

	public Set<Employee>  showems(int id) {
		Set<Employee> ems = null;
		try{
			session=sf.openSession();
			session.beginTransaction();
			Department de=(Department) session.get(Department.class,id);
			ems=de.getMyeEmployees();
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		return ems ;
	}
	
	
}
